from typing import Annotated

from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.ext.asyncio import AsyncSession

from app.core import dependencies
from app.db import session
from app.db.models import user as user_model
from app.schemas import credit as credit_schema
from app.services import credit as credit_service

router = APIRouter(prefix="/credits", tags=["Credits"])

@router.get("/me", response_model=credit_schema.Credit)
async def read_my_credits(
    current_user: Annotated[user_model.User, Depends(dependencies.get_current_user)],
    db: Annotated[AsyncSession, Depends(session.get_db)]
):
    """Fetch the current logged-in user's credit balance."""
    credits = await credit_service.get_user_credits(db, user_id=current_user.id)
    # get_user_credits handles initialization if record doesn't exist
    return credits

# Admin routes for modifying credits will be in admin.py
# TODO: Consider adding an endpoint for users to view their credit transaction history (CreditLog)
